package com.ideal.frame.sys.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.ideal.frame.entity.BaseAutoModel;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
@Entity
@Table(name = "t_sys_user")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class SysUser extends BaseAutoModel {
    @NotBlank(message = "用户名不能为空")
    private String username;
    @JsonIgnore
    private String password;
    private String email;
    private String mobile;
    private String realName;
    private Boolean status;
    @Transient
    private List<Long> roleIds;

    public SysUser(){}

    public SysUser(Long id){
        super(id);
    }

    private SysUser(Builder builder) {
        setId(builder.id);
        setCreateTime(builder.createTime);
        setModifyTime(new Date());
        setCreateUser(builder.createUser);
        setModifyUser(builder.modifyUser);
        setDelFlag(false);
        setUsername(builder.username);
        setPassword(builder.password);
        setEmail(builder.email);
        setMobile(builder.mobile);
        setRealName(builder.realName);
        setStatus(builder.status);
        setRoleIds(builder.roleIds);
    }

    public static final class Builder {
        private Long id;
        private Date createTime;
        private Long createUser;
        private Long modifyUser;
        private @NotBlank(message = "用户名不能为空") String username;
        private String password;
        private String email;
        private String mobile;
        private String realName;
        private Boolean status;
        private List<Long> roleIds;

        public Builder() {
        }

        public Builder id(Long val) {
            id = val;
            return this;
        }

        public Builder createTime(Date val) {
            createTime = val;
            return this;
        }

        public Builder createUser(Long val) {
            createUser = val;
            return this;
        }

        public Builder modifyUser(Long val) {
            modifyUser = val;
            return this;
        }


        public Builder username(@NotBlank(message = "用户名不能为空") String val) {
            username = val;
            return this;
        }

        public Builder password(String val) {
            password = val;
            return this;
        }

        public Builder email(String val) {
            email = val;
            return this;
        }

        public Builder mobile(String val) {
            mobile = val;
            return this;
        }

        public Builder realName(String val) {
            realName = val;
            return this;
        }

        public Builder status(Boolean val) {
            status = val;
            return this;
        }

        public Builder roleIds(List<Long> val) {
            roleIds = val;
            return this;
        }

        public SysUser build() {
            return new SysUser(this);
        }
    }
}
